package com.sauro.util;

import com.sauro.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@SuppressWarnings("serial")
@Slf4j
public abstract class BaseException extends RuntimeException {


    public BaseException() {
    }

    public BaseException(Throwable ex) {
        super(ex);
    }

    public BaseException(String msg) {
        super(msg);
    }

    public BaseException(AbstractCodeUtil codeUtil, Object... args) {
        super(codeUtil.getMsg(args));
    }

    public BaseException(Throwable ex, AbstractCodeUtil codeUtil, Object... args) {
        super(codeUtil.getMsg(args), ex);
    }

    public void handler(Map<String, Object> modelMap) {
        log.info("handler");
        modelMap.put("code", getMsgCode().name());
        log.info(getMsgCode().name());
        if (StringUtils.isNotBlank(getMessage())) {
            modelMap.put("remark", getMessage());
            modelMap.put("message", getMessage());
            log.error("module=业务异常;event=" + getMsgCode().name() + ";msg=" + getMessage());
        } else {
            modelMap.put("remark", getMsgCode().getMsg());
            modelMap.put("message", getMsgCode().getMsg());
            log.error("module=业务异常;event=" + getMsgCode().name() + ";msg=" + getMsgCode().getMsg());
        }

        modelMap.put("timestamp", System.currentTimeMillis());
        log.info("timestamp:" + System.currentTimeMillis());
    }

    /**
     * @return
     */
    protected abstract AbstractCodeUtil getMsgCode();
}
